<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="30%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="70px" :rules="rules">
      <el-form-item label="科室" prop="officename">
        <el-select
          v-model="officename"
          class="m-2"
          placeholder="请选择科室"
          size="large"
        >
          <el-option
            v-for="item in offices"
            :key="item.value"
            :label="item.label"
            :value="item.officename"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医生" prop="dname">
        <el-select
          v-model="form.dname"
          class="m-2"
          placeholder="请选择医生"
          size="large"
        >
          <el-option
            v-for="item in doctors"
            :key="item.value"
            :label="item.label"
            :value="item.dname"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="worktime">
        <div class="demo-date-picker">
          <div class="block">
            <el-date-picker
              v-model="form.worktime"
              type="date"
              placeholder="选择日期"
              :size="size"
              value-format="YYYY-MM-DD"
            />
          </div>
        </div>
      </el-form-item>
      <el-form-item label="时间" prop="time">
        <el-time-picker
          v-model="time"
          is-range
          range-separator="To"
          start-placeholder="Start time"
          end-placeholder="End time"
          value-format="hh:mm"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch } from 'vue'
import { postWorkday } from '@/api/workday'
import i18n from '@/i18n'
import { getOffice } from '@/api/office'
import { getDoctor } from '@/api/yuyue1-1'
import { ElMessage } from 'element-plus'
const props = defineProps({
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogTableValue: {
    type: Object,
    default: () => {}
  }
})

const time = ref([])

const offices = ref([])

const getOffices = async () => {
  const res = await getOffice()
  offices.value = res.data.datas
  console.log(offices)
}
getOffices()

const doctors = ref([])

const officename = ref('')
const formRef = ref(null)
const form = ref({
  dname: '',
  worktime: '',
  time: ''
})
const emits = defineEmits(['update:modelValue', 'initUserList'])

const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = async () => {
  postWorkday(form.value)
  ElMessage({
    message: i18n.global.t('操作成功'),
    type: 'success'
  })
  emits('initUserList')
  handleClose()
}
const rules = ref({
  dname: [
    { required: true, message: 'Please input username', trigger: 'blur' }
  ],
  worktime: [
    { required: true, message: 'Please input worktime', trigger: 'blur' }
  ],
  time: [{ required: true, message: 'Please input time', trigger: 'blur' }]
})

watch(
  () => props.dialogTableValue,
  () => {
    form.value = props.dialogTableValue
  },
  { deep: true, immediate: true }
)
watch(
  () => officename,
  async (oldValue, newValue) => {
    const res = await getDoctor(newValue.value)
    doctors.value = res.data.datas
  },
  { deep: true }
)

watch(
  () => time,
  (oldValue, newValue) => {
    form.value.time = newValue.value[0] + '-' + newValue.value[1]
  },
  { deep: true }
)
</script>

<style></style>